from colorama import init, Fore, Style

# 初始化colorama（Windows下需要）
init(autoreset=True)


def print_colored_results(results):
    """彩色打印测试结果"""
    print("\n=== 测试结果 ===")

    for result in results:
        # 构建状态标记和颜色
        if result['success']:
            status = "✓ 成功"
            color = Fore.GREEN
        else:
            status = "✗ 失败"
            color = Fore.RED

        # 输出带颜色的结果行
        print(f"{color}{status}{Style.RESET_ALL} | {result['name']}")

        # 输出详细信息（如果失败）
        if not result['success']:
            print(f"   URL: {result.get('url', '')}")
            print(f"   错误: {result.get('error', '断言失败')}")
            if 'assertions' in result:
                for assertion in result['assertions']:
                    if not assertion['success']:
                        print(f"   断言失败: {assertion.get('description', '')}")
                        print(f"     预期: {assertion['expected']}, 实际: {assertion.get('actual', '无')}")

    # 统计信息
    passed = sum(1 for r in results if r['success'])
    failed = len(results) - passed
    print(f"\n总计: {Fore.GREEN}{passed}通过{Style.RESET_ALL}, "
          f"{Fore.RED}{failed}失败{Style.RESET_ALL}")